//
//  ShowPicViewController.m
//  BiologyLibrary
//
//  Created by milo on 16/7/5.
//  Copyright © 2016年 youxh. All rights reserved.
//

#import "ShowPicViewController.h"
#import "MyScrollView.h"

@interface ShowPicViewController ()

@end

@implementation ShowPicViewController
#define IMAGE_TAG 10000000

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"%@",_urlarr);
    [self setNaviBarTitle:[NSString stringWithFormat:@"%d of %lu",[_nowpage intValue]+1,(unsigned long)_urlarr.count]];
    mianscroview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, fWidth_Current, fHeight_Current)];
    mianscroview.contentSize=CGSizeMake(fWidth_Current*_urlarr.count, 1);
    mianscroview.delegate=self;
    mianscroview.pagingEnabled=YES;
    mianscroview.backgroundColor=[UIColor blackColor];
    [self.view addSubview:mianscroview];
    
    for (int i=0; i<_urlarr.count; i++) {
        MyScrollView *ascrView = [[MyScrollView alloc] initWithFrame:CGRectMake(fWidth_Current*i, 0, fWidth_Current, fHeight_Current)];
        
        ascrView.tag = IMAGE_TAG+i;
        
        [mianscroview addSubview:ascrView];
        if ([[_urlarr objectAtIndex:i] hasPrefix:@"http"]) {
            [ascrView setImagewithurl:[_urlarr objectAtIndex:i]];
        }else
        {
            [ascrView setImagewithpath:[_urlarr objectAtIndex:i]];
        }
    }

    inpage=[_nowpage intValue];
    [mianscroview setContentOffset:CGPointMake(fWidth_Current*[_nowpage intValue], 0)];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(singleTap:) name:@"singleTap" object:nil];

}
-(void)singleTap:(NSNotification *)notice
{
    
    if (aa==0) {
        aa=1;
        [self hideNaviBar:YES];
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    }else{
        aa=0;
        [self hideNaviBar:NO];
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    //NSLog(@"page = %d, lastPage = %d", page, lastPage);
    if (inpage != page)
    {
        [self setNaviBarTitle:[NSString stringWithFormat:@"%ldof%lu",page+1,(unsigned long)_urlarr.count]];

        MyScrollView *aView = (MyScrollView *)[mianscroview viewWithTag:IMAGE_TAG+page];
        aView.zoomScale = 1.0;
        inpage=page;
        
    }
    
    
    
}


- (void)viewDidUnload
{
    mianscroview = nil;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
